<?php

/***************************************************************\
* FILE:		sys/pages/preferences.inc
* SECTION:	pages > prefs management
* FUNCTION:	
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/


/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("VIEW");
}

function page_inc_execute_action($action) {
	global $PAGE, $CONFIG;

	switch($action) {
		default:
			add_alert("unknown action: $action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE;

	if ($_POST['submitted']) {
		
		include_once('sys/inputcheck.inc');
		
		$ok = 1;
		$ok = (check_int($_POST['pref_numposts'], "aantal posts per pagina") and $ok) ? 1 : 0;
		
		if ($ok) {
			if (!$_POST['skin'])
				errorpage(9);
			if ($_SESSION['user_id']) {
				$_SESSION['member']['pref_skin'] = $_POST['skin'];
				$_SESSION['member']['pref_numposts'] = $_POST['pref_numposts'];
				db_do_query(db_query_update(array('member_since', 'last_visit', 'pref_skin', 'pref_numposts'), array('member_since', 'last_visit', db_sqlize($_POST['skin']), intval($_POST['pref_numposts'])), 'MEMBER', "user_id = {$_SESSION['user_id']}"));
			} else {
				$_SESSION['member']['skin'] = $_POST['skin'];
				$_SESSION['member']['pref_numposts'] = $_POST['pref_numposts'];
			}
			endpage("OK, je voorkeuren zijn aangepast");
		}
	}
	
	$inputs = array();
	
	$inputs[] = form_header("Algemeen");
	
	$inputs[] = form_input_field("# reacties per pagina", "pref_numposts", $_SESSION['member']['pref_numposts']);
	
	$kv = get_keys_and_values("name", "description", "SKIN");
	global $CONFIG;
	$old = $_SESSION['user_id'] ? $_SESSION['member']['pref_skin'] : $_SESSION['skin'];
	$old = $old ? $old : $CONFIG['default_skin'];
	$keys = array();
	$values = array();
	while ($k = array_pop($kv['keys'])) {
		$v = array_pop($kv['values']);
		$keys[] = $k;
		$values[] = "$k - $v";
	}
	$inputs[] = form_input_select("Kies je skin", "skin", $keys, $values, '', $old);
	if (!$_SESSION['user_id'])
		$inputs[] = form_info("De gegevens die je hier aanpast blijven maar geldig gedurende deze sessie. Je kan <a href=/service/.register?action=add>registreren</a> en <a href=.login>inloggen</a> om je instellingen te bewaren.");
	$PAGE['content']['form'] = form_create("prefform", array_reverse($inputs));

	$PAGE['content']['title'] = "Voorkeuren" . ($_SESSION['user_id'] ? ": {$_SESSION['member']['nick']}" : "");
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_body($PAGE['content']['body']);
	print_form($PAGE['content']['form']);
}

?>
